﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using BLL;
using System.Data;
using Model;
using System.Web.Script.Serialization;

namespace WebApplication01.AssetHandel
{
    /// <summary>
    /// CampusHandel 的摘要说明
    /// </summary>
    public class CampusHandel : IHttpHandler
    {
        CampusBLL cambll = new CampusBLL();
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string stype = context.Request["sType"];
            string result = string.Empty;
            if (stype == "list")
            {
                string camName = context.Request["camName"];
                //列表展示
                int offset = Convert.ToInt32(context.Request["offset"]);
                int limit = Convert.ToInt32(context.Request["limit"]);
                int begin = offset + 1;
                int end = offset + limit;
                int total;
                List<CampusInfo> list = cambll.GetCampus(camName, begin, end, out total);
                JavaScriptSerializer s = new JavaScriptSerializer();
                var json = new
                {
                    total = total,
                    rows = list
                };
                result = s.Serialize(json);
              context.Response.Write(result);
            }
            else if (stype == "select")
            {
                //下拉列表
            }
            else if (stype == "update")
            {
                CampusInfo camp = new CampusInfo();
                
                camp.CampusName = context.Request["cName"];
                camp.CampusReMark = context.Request["cBZ"];
                if (!string.IsNullOrEmpty(context.Request["ID"]))
                {
                    camp.CampusID = Convert.ToInt32(context.Request["ID"]);
                    result = cambll.Update(camp);
                }
                else
                {
                    result = cambll.Add(camp);
                }
                context.Response.Write(result);
            }
            else if (stype == "Del")
            {
                int cid= Convert.ToInt32(context.Request["id"]);
                result = cambll.Delete(cid);
                context.Response.Write(result);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}